﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using SpLocalizer.Common.DbApi;
using SpLocalizer.ContentSourceProviders.SpDocumentLibraryFile;
using Microsoft.Practices.Unity;


namespace SpLocalizer.ContentSourceProviders
{

    /// <summary>
    /// Project for translation List item values
    /// </summary>
    public class SpFileTranslationProvider : AbstractTranslationProject, IDisposable
    {

        private SPSite _site = null;
        /// <summary>
        /// Local SharePoint Site project handle
        /// </summary>
        private SPSite Site
        {
            get
            {
                if (this._site == null)
                {
                    this._site = new SPSite(this.ConnectionString);
                }
                return this._site;
            }
        }

        protected override void Bind(bool IncludeDescendants)
        {
            SpDocumentLibraryFileContent firstList = new SpDocumentLibraryFileContent();
            SPWeb parentWeb = this.Site.OpenWeb();
            SPList list = parentWeb.GetList(this.ConnectionString);

            if (list == null) // We have to translate all document libraries recursively
            {
                if (!IncludeDescendants)
                {
                    throw new ApplicationException("To translate all document libraries you have to check IncludeDescendants");
                }

                BindToAllWebDocLibs(parentWeb);
                this.ProjectTitle = String.Format("All document libraries under {0}", this.ConnectionString);
            }
            else
            {
                BindToDocLib(list, parentWeb.ID);
                this.ProjectTitle = String.Format("Document library {0} content", this.ConnectionString);
            }
        }

        private void BindToDocLib(SPList list, Guid parentWeb)
        {
            SpDocumentLibraryFileContent DocLibraryFilesProvider = new SpDocumentLibraryFileContent();
            DocLibraryFilesProvider.Bind(list);
            DocLibraryFilesProvider.ParentContentId = parentWeb.ToString();
            this._ContentData.Add(DocLibraryFilesProvider);
        }

        private void BindToAllWebDocLibs(SPWeb web)
        {
            foreach (SPList list in web.Lists)
            {
                if (list.BaseType == SPBaseType.DocumentLibrary)
                    BindToDocLib(list, web.ID);
            }
            foreach (SPWeb subWeb in web.Webs)
                BindToAllWebDocLibs(subWeb);
        }

        protected override AbstractContent LoadContent(LocalizableContent content)
        {
            SpDocumentLibraryFileContent DocLibraryContent = ServiceContainer.Resolve<AbstractContent>(content.ContentSourceProvider) as SpDocumentLibraryFileContent;
            if (DocLibraryContent != null) // We have only ione content provider 
            {

                DocLibraryContent.PropertiesSerializationXml = content.ContentSourceProviderData;
                DocLibraryContent.Id = content.Id;
                SPWeb listWeb = this.Site.OpenWeb(new Guid(content.ParentContentId));
                SPList DocumentLibrary = listWeb.GetList(content.ContentAddress);

                DocLibraryContent.Bind(DocumentLibrary);
                this._ContentData.Add(DocLibraryContent);
                return DocLibraryContent;
            }
            return null;
        }


        /// <summary>
        /// Content and task provider registration
        /// </summary>
        protected override void RegisterContentProviderTypes()
        {
            ServiceContainer.Container.RegisterType<AbstractContent, SpDocumentLibraryFileContent>(typeof(SpDocumentLibraryFileContent).Name);
            ServiceContainer.Container.RegisterType<BaseTranslationTask, WordFileLocalizationTask>(typeof(WordFileLocalizationTask).Name);
            ServiceContainer.Container.RegisterType<BaseTranslationTask, PowerPointFileLocalizationTask>(typeof(PowerPointFileLocalizationTask).Name);
            ServiceContainer.Container.RegisterType<BaseTranslationTask, ExcelFileLocalizationTask>(typeof(ExcelFileLocalizationTask).Name);
        }

        public void Dispose()
        {
            if (this._site != null)
            {
                this._site.Close();
                this._site = null;
            }
        }
    }
}
